/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.allerj.gui.action;

import it.allerj.bean.AllergicTestFilter;
import it.allerj.bean.PatientFilter;
import it.allerj.common.persistence.OQLHelper;
import it.allerj.common.persistence.PersistenceObject;
import it.allerj.model.AllergicTest;
import it.allerj.model.AllergicTestDoubleFrame;
import it.allerj.model.AllergicTestSingleFrame;
import it.allerj.model.Patient;
import it.allerj.model.RowMatrix;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;

/**
 *
 * @author Alessandro Veracchi
 */
public class GuiAction implements GuiActionInterface{
    public void savePersistenceObject(PersistenceObject pObj){
        OQLHelper.saveOrUpdate(pObj);
    }

    public List<Patient> searchPatient(PatientFilter patientFilter) {
        List<Patient> patientList = null;
        String whereClause = "";
        if(patientFilter!=null){
            String name = patientFilter.getName();
            String surname = patientFilter.getSurname();
            if(name !=null && name.trim().length()>0){
                whereClause = encodeWhereClause(whereClause);
                whereClause += " lower(patient.name) like lower('%"+name+"%')";
            }
            if(surname !=null && surname.trim().length()>0){
                whereClause = encodeWhereClause(whereClause);
                whereClause += " lower(patient.surname) like lower('%"+surname+"%')";
            }
            patientList = (List<Patient>)OQLHelper.createQuery("from "+Patient.class.getName()+" patient "+whereClause).list();
        }
        return patientList;
    }
    private String encodeWhereClause(String whereClause){
        if(whereClause.trim().length()==0){
            whereClause += " where ";
        } else {
            whereClause += " and ";
        }
        
        return whereClause;
    }
    public void deletePersistendeObject(PersistenceObject pObj) {
        OQLHelper.remove(pObj);
    }

    public List<AllergicTest> searchAllergicTest(AllergicTestFilter allergicTestFilter) {
        List<AllergicTest> allergicTestList = null;
        String whereClause = "";
        String queriedClass = AllergicTest.class.getName();
        if(allergicTestFilter!=null){
            String barcode = allergicTestFilter.getBarcode();
            String fromRange = allergicTestFilter.getFromRange();
            String toRange = allergicTestFilter.getToRange();
            Date fromDate = allergicTestFilter.getFromDate();
            Date toDate = allergicTestFilter.getToDate();
            boolean singleFrame = allergicTestFilter.isSingleFrame();
            boolean doubleFrame = allergicTestFilter.isDoubleFrame();
            if(barcode !=null && barcode.trim().length()>0){
                whereClause = encodeWhereClause(whereClause);
                whereClause += " lower(allergicTest.barcode) like lower('%"+barcode+"%')";
            }
            if(fromRange !=null && fromRange.trim().length()>0){
                whereClause = encodeWhereClause(whereClause);
                whereClause += " allergicTest.date "+fromRange+" :fromDate";
            }
            if(toRange !=null && toRange.trim().length()>0){
                whereClause = encodeWhereClause(whereClause);
                whereClause += " allergicTest.date  "+toRange+" :toDate";
            }
            if(!singleFrame || !doubleFrame){
                if(singleFrame){
                    queriedClass =AllergicTestSingleFrame.class.getName();
                }
                if(doubleFrame){
                    queriedClass =AllergicTestDoubleFrame.class.getName();
                }
            }
            Query query = OQLHelper.createQuery("from "+queriedClass+" allergicTest "+whereClause);
            if(fromRange !=null && fromRange.trim().length()>0){
               query.setDate("fromDate", fromDate);
            }
            if(toRange !=null && toRange.trim().length()>0){
                query.setDate("toDate", toDate);
            }
            allergicTestList = (List<AllergicTest>)query.list();
        }
        return allergicTestList;
    }

    public <T> T getPersistenceObject(Class<T> clazz, int id) {
        return (T)OQLHelper.get(clazz.getName(), id);
    }

    public <T> List<T> getPersistenceObjectList(Class<T> clazz) {
        return (List<T>)OQLHelper.getListOf(clazz);
    }

    public <T> T getPersistenceObjectByStringAttribute(Class<T> clazz, String nameProperty, String value) {
        return (T)OQLHelper.createQuery("from "+clazz.getName()+" as obj where obj."+nameProperty+" = :value").setString("value", value).uniqueResult();
    }

    
}
